<template>
	<view class="farmer-order-distribution-list">
		<fixed>
			<tab @change="handerTabChange" :list="tabList" :activeIndex="activeIndex" :activeColor="primaryColor"
				:width="100/tabList.length + '%'" height="100rpx"></tab>
			<view class="b-1px-b"></view>
		</fixed>
		<view @tap.stop="goDetail(index)"
			class="order-item fill-base mg-big pd-lg f-paragraph c-title radius-16 box-shadow"
			v-for="(item,index) in list.data" :key="index">
			<view class="flex-between pb-lg b-1px-b">
				<view class="c-desc max-500 ellipsis">配送单号：{{item.order_code}}</view>
				<view :style="{color:item.pay_type == 2? subColor: item.pay_type == 3? primaryColor: ''}">
					{{statusType[item.pay_type]}}
				</view>
			</view>
			<view class="flex-warp mt-lg">
				<view class="text-item c-desc">订单类型</view>
				<view class="flex-1">{{orderType[item.type]}}</view>
			</view>
			<view class="flex-warp mt-md">
				<view class="text-item c-desc">{{item.send_type == 1 ? '自提时间':'送货时间'}}</view>
				<view class="flex-1">{{item.time_text}}</view>
			</view>
			<view class="flex-warp mt-md">
				<view class="text-item c-desc">配送方式</view>
				<view class="flex-1">{{sendType[item.send_type]}}</view>
			</view>
			<view class="flex-warp mt-md" v-if="item.send_type == 2">
				<view class="text-item c-desc">收货地址</view>
				<view class="flex-1">{{item.address}}</view>
			</view>
			<view class="flex-warp mt-md">
				<view class="text-item c-desc">{{item.send_type==1?'自提人':'收货人'}}</view>
				<view class="flex-1">{{`${item.user_name} ${item.mobile}`}}</view>
			</view>
			<view class="flex-warp pt-lg" v-if="item.pay_type > 2 && item.send_type == 2">
				<view class="text-item c-desc">发货时间</view>
				<view class="flex-1">{{item.send_time}}</view>
			</view>
			<view class="flex-warp pt-lg" v-if="item.pay_type == 7">
				<view class="text-item c-desc">{{item.send_type == 1 ? '取货时间':'收货时间'}}</view>
				<view class="flex-1">{{item.receiving_time}}</view>
			</view>
			<view class="mt-md" v-if="item.text">
				<view class="text-item c-desc">备注</view>
				<view class="mt-sm">
					<text decode="emsp" style="word-break:break-all;">{{item.text}}</text>
				</view>
			</view>
			<block v-if="item.send_type == 2">
				<view class="flex-warp pt-lg">
					<view class="text-item c-desc">付款单号</view>
					<view class="flex-1">{{item.transaction_id}}</view>
				</view>
				<view class="flex-warp pt-lg" v-if="item.pay_type == -1">
					<view class="text-item c-desc">退款单号</view>
					<view class="flex-1">{{item.refund_code}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view class="text-item c-desc">配送费</view>
					<view class="flex-1 flex-y-center c-warning">
						<view>¥{{item.pay_price}}</view>
						<view class="ml-sm" v-if="item.pay_type == -1">(已退)</view>
					</view>
				</view>
			</block>
			<view class="flex-between mt-md" v-if="item.pay_type == 2">
				<view></view>
				<view @tap.stop="toChangeOrder(index,item.send_type==1?'confirm_get_item':'confirm_send_item')"
					class="common-btn flex-center f-caption c-base radius-4 ml-lg" :style="{background:primaryColor}">
					{{item.send_type == 1?'确认取货':'立即发货'}}
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<view class="space-footer"></view>

		<common-popup @confirm="confirmChangeOrder" ref="confirm_get_item" title="确认取货" desc="请确认是否取货，确认后将无法恢复"
			text="确认取货" :info="popupInfo">
		</common-popup>
		<common-popup @confirm="confirmChangeOrder" ref="confirm_send_item" title="立即发货" desc="请确认是否发货，确认后将无法恢复"
			text="确认发货" :info="popupInfo">
		</common-popup>
	</view>
</template>

<script>
	import {
		mapState,
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				activeIndex: 0,
				tabList: [{
					id: 0,
					title: '全部'
				}, {
					id: 2,
					title: '待配送',
					number: 0
				}, {
					id: 3,
					title: '配送中',
					number: 0
				}, {
					id: 7,
					title: '已完成'
				}, {
					id: -1,
					title: '已取消'
				}],
				statusType: {
					'-1': '已取消',
					2: '待配送',
					3: '配送中',
					7: '已完成',
				},
				orderType: {
					1: '认养订单',
					2: '土地订单'
				},
				sendType: {
					1: '自提',
					2: '快递'
				},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				popupInfo: {},
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.$util.showLoading()
			this.options = options
			let {
				tab = 0
			} = options
			this.activeIndex = tab
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			initIndex() {
				this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					param,
					tabList,
					activeIndex
				} = this
				param.pay_type = tabList[activeIndex].id
				let newList = await this.$api.farmer.farmerSendOrderList(param);

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.tabList[1].number = newList.no_send
				this.tabList[2].number = newList.send_ing
				this.$util.hideAll()
			},
			handerTabChange(index) {
				this.activeIndex = index
				this.$util.showLoading()
				this.param.page = 1
				this.list.data = []
				this.getList()
			},
			// 确认取货/开始配送/配送到家
			async toChangeOrder(index, key) {
				let {
					id,
					order_code,
				} = this.list.data[index]
				this.popupInfo = {
					id,
					name: `订单编号：${order_code}`,
					image: '',
					index,
					refs_key: key
				}
				this.$refs[key].open()
			},
			async confirmChangeOrder() {
				let {
					id,
					index,
					refs_key
				} = this.popupInfo
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				let methodModel = {
					'confirm_get_item': {
						method: 'sendOrderPickup',
						msg: '取货成功'
					},
					'confirm_send_item': {
						method: 'sendOrderSend',
						msg: '发货成功'
					}
				}
				let {
					method,
					msg,
				} = methodModel[refs_key]
				try {
					await this.$api.farmer[method]({
						id
					})
					this.$util.hideAll()
					this.$util.showToast({
						title: msg
					})
					this.lockTap = false
					this.$refs[refs_key].close()
					this.initRefresh()
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			// 订单详情
			goDetail(index) {
				let {
					id,
					type
				} = this.list.data[index]
				let url = `/farmer/pages/order/distribution/detail?id=${id}&type=${type}`
				this.$util.goUrl({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
</style>
